
package com.j2eefast.produce.pms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.j2eefast.common.core.base.entity.Ztree;
import com.j2eefast.common.core.exception.RxcException;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.MapUtil;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.common.core.utils.SpringUtil;
import com.j2eefast.common.core.utils.ToolUtil;
import com.j2eefast.framework.annotation.DataFilter;
import com.j2eefast.framework.sys.entity.SysUserEntity;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import com.j2eefast.produce.pms.entity.PmsParticipantEntity;
import com.j2eefast.produce.pms.entity.PmsTestEntity;
import com.j2eefast.produce.pms.mapper.PmsParticipantMapper;
import com.j2eefast.produce.pms.mapper.PmsTestMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>检验标准</p>
 *
 * @author: summer
 * @date: 2021-05-29 16:47
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Service
@Transactional(readOnly = true)
public class PmsTestService extends ServiceImpl<PmsTestMapper, PmsTestEntity> {

	@Resource
	protected PmsTestMapper pmsTestMapper;


	public List<PmsTestEntity> findList(Map<String, Object> params) {
		return SpringUtil.getAopProxy(this).getDeptList(params);
	}
	@DataFilter(compAlias="c",deptAlias = "c")
	public List<PmsTestEntity> getDeptList(Map<String, Object> params){
		String className = (String)params.get("className");
		String parentId =(String)params.get("parentId");

		return this.baseMapper.getDeptList(
//                StrUtil.nullToDefault(className,""),
//                StrUtil.nullToDefault(parentId,""),
				(String) params.get(Constant.SQL_FILTER));
	}
	public PmsTestEntity findCompById(Long id) {
		return pmsTestMapper.findCompById(id);
	}
	/**
	 * 查询公司树
	 * @return
	 */
	public List<Ztree> findCompTree(Long parentId) {
		Map<String,Object> mapp = new HashMap<>();
		mapp.put("parentId",parentId);
		List<PmsTestEntity> list = SpringUtil.getAopProxy(this).getDeptList(mapp);
		List<Ztree> ztrees = initZtree(list);
		return ztrees;
	}
	public List<Ztree> initZtree(List<PmsTestEntity> list) {
		return  initZtree(list,null);
	}
	public List<Ztree> initZtree(List<PmsTestEntity> compList, List<String> roleCompList){

		List<Ztree> ztrees = new ArrayList<Ztree>();
		boolean isCheck = !ToolUtil.isEmpty(roleCompList);
		for (PmsTestEntity comp : compList) {
			Ztree ztree = new Ztree();
			ztree.setId(comp.getId());
			ztree.setpId(comp.getParentId());
			ztree.setName(comp.getTestName());
			ztree.setTitle(comp.getTestName());
			ztrees.add(ztree);
		}
		return ztrees;
	}

	public void checkDataScope(Long... ids) {
//        if (!UserUtils.hasAnyRoleKeys(Constant.SU_ADMIN)) {
		//check current user has data scope for the deleted use
		Map<String, Object> params = Maps.newConcurrentMap();
		params.put("ids", ids);
		List<?> listData =SpringUtil.getAopProxy(this).getDeptList(params);
		if (null == listData || listData.size() == 0 || listData.size() != ids.length) {
			throw new RxcException(ToolUtil.message("illegal request"),"50001");
		}
//        }
	}

	public boolean checkCompNameUnique(PmsTestEntity comp) {
		Long compId = ToolUtil.isEmpty(comp.getId()) ? -1L : comp.getId();
		PmsTestEntity info = this.getOne(new QueryWrapper<PmsTestEntity>().
				eq("id",comp.getId()).eq("parent_id",comp.getParentId()));
		if (ToolUtil.isNotEmpty(info) && info.getId().longValue() != compId.longValue())
		{
			return  false;
		}
		return true;
	}

	@Transactional(rollbackFor = Exception.class)
	public boolean add(PmsTestEntity comp){
		Date date = new Date();//创建一个时间对象，获取到当前的时间
		comp.setCreateTime(date);
		String userId= UserUtils.getUserInfo().getName();
		comp.setCreateBy(userId);
		if(this.save(comp)){
			return true;
		}
		return false;
	}
	@Transactional(rollbackFor = Exception.class)
	public boolean update(PmsTestEntity comp){
		if(this.updateById(comp)){
			return true;
		}
		return false;
	}
	/**
	 * 通过id删除
	 * @param
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public boolean delSysCompById(Long id) {

		// 先判断是否有子公司
		List<PmsTestEntity> list = this.listByMap(new MapUtil().put("parent_id", id));
		if (ToolUtil.isNotEmpty(list)) {
			throw new RxcException("请先删除子分类","50001");
		}
		// 在判断公司是否有分配到用户上面如果改公司已经分配到用户上,先删除用户在删
		List<PmsTestEntity> users = pmsTestMapper.listByMap(id);
		if (ToolUtil.isNotEmpty(users)) {
			throw new RxcException("请先删除关联分类","50001");
		}
		//删除关联地区
		//   busClassifyMapper.removeByMap(id);

		if(this.removeById(id)){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 页面展示查询翻页
	 */
	public PageUtil findPage(Map<String, Object> params) {
//		String sampleCode = (String) params.get("sampleCode");
//		String sampleType = (String) params.get("sampleType");


		Page<PmsTestEntity> page = pmsTestMapper.findPage(
				new Query<PmsTestEntity>(params).getPage(),
//				StrUtil.nullToDefault(sampleCode,""),
//				StrUtil.nullToDefault(sampleType,""),
				(String) params.get(Constant.SQL_FILTER));
		return new PageUtil(page);
	}
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteByIds(Long[] ids) {

		return pmsTestMapper.deleteByIds(ids);

	}



//	@Transactional(rollbackFor = Exception.class)
//	public boolean updateCheck(Long id) {
//		Long userId= UserUtils.getUserInfo().getId();
//
//		return pmsTestMapper.updateCheck(id ,userId);
//
//	}
//	public SysUserEntity getSysById(@Param("userId")Long userId){
//		return pmsTestMapper.getSysById(userId);
//	}

//	/**
//	 * 添加到账认领信息
//	 *
//	 */
//	@Transactional(rollbackFor = Exception.class)
//	public boolean save(PmsParticipantEntity taskFrom){
////		Long userId= UserUtils.getUserInfo().getId();
//		Date date = new Date();//创建一个时间对象，获取到当前的时间
//		taskFrom.setCreateTime(date);
//		taskFrom.setUpdateTime(date);
//
//		String userName=UserUtils.getUserInfo().getName();
//		taskFrom.setCreateBy(userName);
//		taskFrom.setUpdateBy(userName);
//
//		//查询工程编号
//		PmsParticipantEntity pmsParticipantEntity = pmsTestMapper.getParticipant();
//		String code=participantCode(pmsParticipantEntity,1);
//		taskFrom.setProjectCode(code);
//		PmsParticipantEntity pmsParticipantEntity1 =pmsTestMapper.getParticipantCode();
//		String code1=participantCode(pmsParticipantEntity1,2);
//
//
//
//		taskFrom.setParticipantCode(code1);
//		return pmsTestMapper.save(taskFrom);
//
//	}
//
//
//	/**
//	 * 生成编号
//	 * @param
//	 * @return
//	 */
//	public String participantCode(PmsParticipantEntity pmsParticipantEntity ,int index) {
////		int id = ParamUtils.getInt(s, "-", 0);
//		// 对当前日期进行格式化
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHss");
//		String date = sdf.format(new Date());
//		//DecimalFormat dft = new DecimalFormat(FORMAT_CODE);
//		//StringUtils ParamUtils = new StringUtils();
////		StringBuilder builder = new StringBuilder();
//		System.out.println("111111111111");
//		System.out.println(pmsParticipantEntity != null);
//		System.out.println("111111111111");
//		if (index == 1) {
//			if ( pmsParticipantEntity != null) {
//				String[] str = pmsParticipantEntity.getProjectCode().split("-");
//				int[] num = new int[str.length];
//				for (int i = 0; i < num.length; i++) {
//					num[i] = Integer.parseInt(str[i]);
//				}
//				int i = num[num.length - 1];
//				StringBuilder builder = new StringBuilder();
//				builder.append("GC-");
//				builder.append(date);
//				builder.append("-");
//				builder.append(i + 1);
//				//String code = ParamUtils.getConteactNo(date, i);
//				return builder.toString();
//			} else {
//				int num = 1;
//				StringBuilder builder = new StringBuilder();
//				builder.append("GC-");
//				builder.append(date);
//				builder.append("-");
//				builder.append(num);
//				//String code = ParamUtils.getConteactNo(date, i);
//				return builder.toString();
//			}
//		}
//		else if (index == 2) {
//				if ( pmsParticipantEntity != null) {
//					String[] str = pmsParticipantEntity.getParticipantCode().split("-");
//					int[] num = new int[str.length];
//					for (int i = 0; i < num.length; i++) {
//						num[i] = Integer.parseInt(str[i]);
//					}
//					int i = num[num.length - 1];
//					StringBuilder builder = new StringBuilder();
//					builder.append("CJ-");
//					builder.append(date);
//					builder.append("-");
//					builder.append(i + 1);
//					//String code = ParamUtils.getConteactNo(date, i);
//					return builder.toString();
//				} else {
//					int num = 1;
//					StringBuilder builder = new StringBuilder();
//					builder.append("CJ-");
//					builder.append(date);
//					builder.append("-");
//					builder.append(num);
//					//String code = ParamUtils.getConteactNo(date, i);
//					return builder.toString();
//				}
//
//		}else
//			return "";
//	}
}
